/**
 * author      : dormi330
 * date        : 2018/6/28
 * project     : mybatis
 * description : TODO
 */

package com.deep.trade.common;

import com.deep.trade.infrastructure.utils.CommonUtils;
import com.google.gson.Gson;

public class HttpResonsePayload<T> {

    private String request_id;
    private String code;
    private String msg;
    private T payload;

    public static <T> HttpResonsePayload happy(T payload) {
        return new HttpResonsePayload<T>(payload);
    }

    public static <T> HttpResonsePayload happy() {
        return new HttpResonsePayload<T>(null);
    }

    public static <T> HttpResonsePayload unhappy(String code, String msg) {
        return new HttpResonsePayload(code, msg);
    }

    public HttpResonsePayload(T payload) {
        request_id = CommonUtils.uuid();
        this.code = "0";
        this.msg = null;
        this.payload = payload;
    }

    public HttpResonsePayload(String code, String msg) {
        request_id = CommonUtils.uuid();
        this.code = code;
        this.msg = msg;
        this.payload = null;
    }

    @Override
    public String toString() {
        return new Gson().toJson(this);
    }

    // === setter/getter ===

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getPayload() {
        return payload;
    }

    public void setPayload(T payload) {
        this.payload = payload;
    }

    public String getRequest_id() {
        return request_id;
    }

    public void setRequest_id(String request_id) {
        this.request_id = request_id;
    }
}
